//
//  PlainDetailViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-3.
//
//

#import "PlainDetailViewController.h"
#import "PassengerViewController.h"

#define BUTTONTAG 29990

@interface PlainDetailViewController ()

@end

@implementation PlainDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化视图
- (void)init_view
{
    //定义title
    self.title = @"机票";
    //加载背影
     [self.view setBackgroundColor:BackgroundColor];

    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 1000);
    [myscrollview setContentSize:newSize];
    
    UIControl *control = [[[UIControl alloc] initWithFrame:myscrollview.frame] autorelease];
    [myscrollview addSubview:control];
    [control addTarget:self action:@selector(onBackgroudTouch) forControlEvents:UIControlEventTouchDown];

    
    
    //第一格
    [self init_titile_cell:myscrollview];
    
    //加载机票
    [self addticket:0 desc:@"成人票" price:@"610" portPrice:@"460" oilPrice:@"50" allPrice:@"100" myscollview:myscrollview];
    [self addticket:1 desc:@"儿童票" price:@"610" portPrice:@"460" oilPrice:@"50" allPrice:@"100" myscollview:myscrollview];
    [self addticket:2 desc:@"婴儿票" price:@"610" portPrice:@"460" oilPrice:@"50" allPrice:@"100" myscollview:myscrollview];
    
    [self.view addSubview:myscrollview];
}

//机票描述
- (void)init_titile_cell:(UIScrollView*)myscollview
{
    //加载白色前景
    UILabel *bgLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 120)];
    bgLbl.backgroundColor = [UIColor whiteColor];
    [myscollview addSubview:bgLbl];

    //加载图片
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rili_03"]];
    imageview.frame = CGRectMake(20, 20, 90, 100);
    [myscollview addSubview:imageview];
    
    //加载日期
    UILabel *dateLbl = [[UILabel alloc]initWithFrame:CGRectMake(35, 30, 85, 30)];
    dateLbl.font = [UIFont systemFontOfSize:16];
    dateLbl.textColor = [UIColor whiteColor];
    dateLbl.backgroundColor = [UIColor clearColor];
    dateLbl.text = @"2013-06";
    [myscollview addSubview:dateLbl];
    
    //加载时间
    UILabel *timeLbl = [[UILabel alloc]initWithFrame:CGRectMake(38, 52, 82, 70)];
    timeLbl.font = [UIFont systemFontOfSize:50];
    timeLbl.textColor = [UIColor whiteColor];
    timeLbl.backgroundColor = [UIColor clearColor];
    timeLbl.text = @"22";
    [myscollview addSubview:timeLbl];
    
    //加载详情
    NSString *lineStr = [NSString stringWithFormat:@"%@ --> %@", @"上海", @"广州"];
    NSString *airinfoStr = [NSString stringWithFormat:@"%@  %@", @"吉祥", @"CA1866"];
    NSString *startInfoStr = [NSString stringWithFormat:@"起飞  %@  %@", @"08：35", @"上海机场"];
    NSString *arriveInfoStr = [NSString stringWithFormat:@"到达  %@  %@", @"12：35", @"白云机场"];
    NSString *seatInfoStr = [NSString stringWithFormat:@"机型  %@  舱位  %@", @"321", @"60折经济舱"];
    
    UILabel *airInfoLbl = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 180, 120)];
    airInfoLbl.lineBreakMode = UILineBreakModeWordWrap;
    airInfoLbl.numberOfLines = 0;
    airInfoLbl.font = [UIFont systemFontOfSize:13];
    airInfoLbl.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@", lineStr, airinfoStr, startInfoStr, arriveInfoStr, seatInfoStr];
    [myscollview addSubview:airInfoLbl];
    
}

//机票内容
- (void)addticket:(NSInteger)index desc:(NSString*)desc price:(NSString*)price portPrice:(NSString*)portPrice oilPrice:(NSString*)oilPrice allPrice:(NSString*)allPrice myscollview:(UIScrollView*)myscollview
{
    //加载背影
    UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    bgBtn.frame = CGRectMake(10, 140+index*90, 300, 80);
    [bgBtn setBackgroundImage:[UIImage imageNamed:@"couponbg"] forState:UIControlStateNormal];
    bgBtn.tag = BUTTONTAG + index;
    [myscollview addSubview:bgBtn];
    //添加button方法
    [bgBtn addTarget:self action:@selector(detailEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    //描述
    UILabel *descLbl = [[[UILabel alloc]initWithFrame:CGRectMake(20, 140+index*90, 90, 40)]autorelease];
    descLbl.font = [UIFont systemFontOfSize:20];
    descLbl.textAlignment=NSTextAlignmentCenter;
    descLbl.backgroundColor = [UIColor clearColor];
    descLbl.text = desc;
    [myscollview addSubview:descLbl];
    
    //价格
    UILabel *priceLbl = [[[UILabel alloc]initWithFrame:CGRectMake(20, 180+index*90, 90, 40)]autorelease];
    priceLbl.font = [UIFont systemFontOfSize:25];
    priceLbl.textAlignment=NSTextAlignmentCenter;
    priceLbl.textColor = [UIColor redColor];
    priceLbl.backgroundColor = [UIColor clearColor];
    priceLbl.text = [NSString stringWithFormat:@"￥%@", price];;
    [myscollview addSubview:priceLbl];
    
    //详情
    NSString *priceStr = [NSString stringWithFormat:@"票价：￥%@", price];
    NSString *portStr = [NSString stringWithFormat:@"机场建设费：￥%@", portPrice];
    NSString *oilStr = [NSString stringWithFormat:@"燃油附加费：￥%@", oilPrice];
    NSString *allStr = [NSString stringWithFormat:@"%@\n%@\n%@", priceStr, portStr, oilStr];
    
    UILabel *detailLbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 140+index*90, 180, 80)]autorelease];
    detailLbl.lineBreakMode = UILineBreakModeWordWrap;
    detailLbl.numberOfLines = 0;
    detailLbl.font = [UIFont systemFontOfSize:14];
    detailLbl.backgroundColor = [UIColor clearColor];
    detailLbl.text = allStr;
    [myscollview addSubview:detailLbl];
    
}

//跳转
- (void)detailEvent:(id)sender
{
    UIButton *button = (UIButton *)sender;
    PassengerViewController *passengerController = [[PassengerViewController alloc]init];
    passengerController.yearType = button.tag % BUTTONTAG + 1;
    [self.navigationController pushViewController:passengerController animated:YES];
    [passengerController release];
}

@end





